﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NhaCCDAO
    {
        public List<NhaCCDTO> LayNhaCC()
        {
            string sql = "SELECT * FROM NhaCungCap WHERE Deleted = 0";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                List<NhaCCDTO> ListNhaCC = new List<NhaCCDTO>();
                while (read.Read())
                {
                    NhaCCDTO NhaCC = new NhaCCDTO();
                    NhaCC.MaNCC = read.GetInt32(0);
                    NhaCC.TenNCC = read.GetString(1);
                    ListNhaCC.Add(NhaCC);
                }
                conn.Close();
                return ListNhaCC;
            }
        }
        public bool ThemNhaCC(string TenNCC)
        {
            string sql = "INSERT INTO NhaCungCap(TenNCC, Deleted) VALUES(N'"+ TenNCC +"', 0)";
            using (SqlConnection conn = DataProvider.ConnectDB())
            { 
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;
            }
        }
        public bool CapNhapNhaCC(string TenNCCCu, string TenNCCMoi)
        {
            string sql = "UPDATE NhaCungCap SET TenNCC = N'" + TenNCCMoi + "' WHERE TenNCC = N'" + TenNCCCu + "' AND Delete=0";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 1)
                    return true;
                return false;                
            }
        }
        public NhaCCDTO LayNCCTheoMa(int MaNCC)
        {
            string sql = "SELECT * FROM NhaCungCap WHERE MaNCC = " + MaNCC;
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();                
                read.Read();
                NhaCCDTO ncc = new NhaCCDTO();
                ncc.MaNCC = read.GetInt32(0);
                ncc.TenNCC = read.GetString(1);
                conn.Close();
                return ncc;
            }
        }
        public bool XoaNhaCC(string TenNhaCC)
        {
            string sql = "UPDATE NhaCungCap SET Delete=1 WHERE TenNCC = N'"+TenNhaCC+"'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 1)
                    return true;
                return false;
            }
        }
    }
}